﻿using System;
using System.Collections.Generic;
using NUnit.Framework;
using Xenta.Enums;
using Xenta.Operations;
using Xenta.Utils;

namespace Xenta.Tests.Operations
{
    [TestFixture]
    public class MeasureUnitTests : OperationTest
    {
        protected override IEnumerable<Type> ModelMappings
        {
            get
            {
                return NHMappings.ALL;
            }
        }

        [TestCase("xy", "name", 1, "format")]
        public void Can_CRUD(string code,
            string name, 
            decimal ratio, 
            string valueFormat)
        {
            var unit = Processor.Execute(new CreateMeasureUnit
            {
                Code = code,
                Name = name,
                Ratio = ratio,
                ValueFormat = valueFormat,
                Flags = MeasureUnitFlags.Information
            }).Result;

            Assert.NotNull(unit);
            unit.Code.AreEqualIgnoringCase(code);
            unit.Name.AreEqualIgnoringCase(name);
            unit.Ratio.AreEqual(ratio);
            unit.ValueFormat.AreEqualIgnoringCase(valueFormat);
            unit.AreEqual(Processor.Execute(new GetMeasureUnit
            {
                EntityID = unit.EntityID
            }).Result);
            unit.AreEqual(Processor.Execute(new GetMeasureUnitByCode
            {
                Code = code
            }).Result);

            code = code.Reverse();
            name = name.Reverse();
            ratio = ratio + 1;
            valueFormat = valueFormat.Reverse();
            var upd_unit = Processor.Execute(new UpdateMeasureUnit
            {
                EntityID = unit.EntityID,
                Code = code,
                Name = name,
                Ratio  = ratio,
                ValueFormat = valueFormat,
                Flags = MeasureUnitFlags.Information | MeasureUnitFlags.Active
            }).Result;

            Assert.NotNull(upd_unit);
            upd_unit.AreEqual(unit);
            upd_unit.Code.AreEqualIgnoringCase(code);
            upd_unit.Name.AreEqualIgnoringCase(name);
            upd_unit.Ratio.AreEqual(ratio);
            upd_unit.ValueFormat.AreEqualIgnoringCase(valueFormat);
            upd_unit.Flags.AreEqual(MeasureUnitFlags.Information | MeasureUnitFlags.Active);

            Processor.Execute(new DeleteMeasureUnit
            {
                EntityID = unit.EntityID
            });
        }

        [Test]
        public void Cannot_Create_If_Code_Empty()
        {
            Assert.Throws<ApplicationException>(() => Processor.Execute(new CreateMeasureUnit
            {
                Code = String.Empty
            }));
        }

        [Test]
        public void Cannot_Update_If_Code_Empty()
        {
            Assert.Throws<ApplicationException>(() => Processor.Execute(new UpdateMeasureUnit
            {
                Code = String.Empty
            }));
        }

        [TestCase(0)]
        [TestCase(-1)]
        public void Cannot_Create_If_Ratio_EqOrLt_Zero(decimal ratio)
        {
            Assert.Throws<ApplicationException>(() => Processor.Execute(new CreateMeasureUnit
            {
                Code = "xy",
                Ratio = ratio
            }));
        }

        [TestCase(0)]
        [TestCase(-1)]
        public void Cannot_Update_If_Ratio_EqOrLt_Zero(decimal ratio)
        {
            Assert.Throws<ApplicationException>(() => Processor.Execute(new UpdateMeasureUnit
            {
                Code = "xy",
                Ratio = ratio
            }));
        }
    }
}
